/* eslint-disable */
const appWakeupLinks = {
    app: 'faceshow://',
    openUrl(fun, params) {
        return `faceshow://sanrenxing.com/call?func=${fun}&params=${JSON.stringify(params)}`
    }
}

const downUrl = {
    ios: "https://apps.apple.com/cn/app/研修宝-学员端/id1287430670",
    android: "http://qr1.weigongju.org/16900010",
    tencent: "https://a.app.qq.com/o/simple.jsp?pkgname=com.yanxiu.gphone.faceshow&g_f=1000047"
}

function getPlatform() {
    let ua = window.navigator.userAgent.toLowerCase();
    if (/(iPhone|iPad|iPod|iOS)/i.test(navigator.userAgent)) {
        return 'ios';
    } else if (ua.indexOf('mqqbrowser') > -1 && ua.indexOf(" qq") < 0) {
        return 'android'
    } else {
        return 'tencent';
    }
}

/**
 * openApp  唤醒App || 下载
 * @param {Object} payload => {
 * fun   String 唤醒方法，不填默认打开App，
 * params String 唤醒携带参数
 * iosDownUrl  String ios下载地址   可直接唤醒 apple store 
 * androidDownUrl  String android下载地址
 * tencentDownUrl  String 腾讯有关浏览器下载地址
 */

function downApp() {
    let platform = getPlatform();
    window.setTimeout(function () {
        window.location.href = downUrl[platform];
    }, 1500);
}


function openApp(fun = '', params = {}, iosDownUrl = "https://apps.apple.com/cn/app/研修宝-学员端/id1287430670", androidDownUrl = "http://qr1.weigongju.org/16900010", tencentDownUrl = "https://a.app.qq.com/o/simple.jsp?pkgname=com.yanxiu.gphone.faceshow&g_f=1000047") {
    let openUrl = fun ? appWakeupLinks.openUrl(fun, params) : appWakeupLinks['app'];
    if (/(iPhone|iPad|iPod|iOS)/i.test(navigator.userAgent)) {
        /* checkOpen((opened) => {
            if (!opened) {
                var timer = window.setTimeout(function () {
                        window.location.href = iosDownUrl;
                    }, 1500);
                }
            })
        // 设置隐藏属性和改变可见属性的事件的名称
        var hidden, visibilityChange;
        if (typeof document.hidden !== "undefined") { // Opera 12.10 and Firefox 18 and later support
            hidden = "hidden";
            visibilityChange = "visibilitychange";
        } else if (typeof document.msHidden !== "undefined") {
            hidden = "msHidden";
            visibilityChange = "msvisibilitychange";
        } else if (typeof document.webkitHidden !== "undefined") {
            hidden = "webkitHidden";
            visibilityChange = "webkitvisibilitychange";
        }
        function handleVisibilityChange() {
            if (document[hidden] && timer) {
                clearInterval(timer)
            }
        } */
        // 处理页面可见属性的改变
        // document.addEventListener(visibilityChange, handleVisibilityChange, false);
        window.location.href = openUrl;

        // 在iframe 中打开APP
        // var ifr = document.createElement('iframe');
        // ifr.src = openUrl;
        // ifr.style.display = 'none';
        // ifr.onload = function(e){
        //   var e = e || window.event;
        //   window.location.href = openUrl
        //  }
        //  ifr.onerror = function(){
        //   window.location.href = iosDownUrl
        //  }
        //  document.body.appendChild(ifr);
    } else if (/(Android)/i.test(navigator.userAgent)) {
        console.log('------------');
        console.log(openUrl);
        console.log(androidDownUrl);
        console.log('------------');
        window.location.href = openUrl;
        /* checkOpen((opened) => {
            if (!opened) {
                var ua = navigator.userAgent.toLowerCase();
                if (ua.indexOf('mqqbrowser') > -1 && ua.indexOf(" qq") < 0) {
                    //qq浏览器
                    window.setTimeout(function () {
                        window.location.href = tencentDownUrl;
                    }, 1500);
                    return;
                }else{
                    window.setTimeout(function () {
                        window.location.href = androidDownUrl;
                    }, 1500);
                }
            }
        }); */

    } else {
        return "PC";
    }
}

// 检查app是否打开  1500s
function checkOpen(cb) {
    const clickTime = +(new Date());

    function check(elsTime) {
        if (elsTime > 2000) {
            cb((document.hidden || document.webkitHidden) ? true : false);
        } else {
            cb(false);
        }
    }

    // 启动间隔20ms运行的定时器，并检测累计消耗时间是否超过1500ms，超过则结束
    let count = 0;
    let intHandle = null;
    intHandle = setInterval(() => {
        count++;
        const elsTime = +(new Date()) - clickTime;
        if (count >= 100 || elsTime > 2000) {
            clearInterval(intHandle);
            check(elsTime);
        }
    }, 20);
}

function isInWechat() {
    var ua = window.navigator.userAgent.toLowerCase();
    if (ua.match(/MicroMessenger/i) == "micromessenger") {
        return true;
    }
    return false;
}

function appDownloadUrl() {
    if (/(iPhone|iPad|iPod|iOS)/i.test(navigator.userAgent)) {
        return "https://apps.apple.com/cn/app/研修宝-学员端/id1287430670";
    } else if (/(Android)/i.test(navigator.userAgent)) {
        return "https://a.app.qq.com/o/simple.jsp?pkgname=com.yanxiu.gphone.faceshow&g_f=1000047";
    }
    return "https://ipx.yanxiu.com/";
}

function isMobile() {
    let flag = navigator.userAgent.match(/(phone|pad|pod|iPhone|iPod|ios|iPad|Android|Mobile|BlackBerry|IEMobile|MQQBrowser|JUC|Fennec|wOSBrowser|BrowserNG|WebOS|Symbian|Windows Phone)/i)
    if (flag) {
        return flag[0] == "iPhone" || flag[0] == "Android";
    } else {
        return false;
    }
}


let util = {
    openApp,
    isMobile,
    appDownloadUrl,
    isInWechat,
    checkOpen,
    downApp,
    getPlatform
}

export default util;